package com.mashang.stuManSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mashang.stuManSystem.entity.Clazz;
import com.mashang.stuManSystem.entity.vo.ClazzVo;
import com.mashang.stuManSystem.service.IClazzService;
import com.mashang.stuManSystem.utils.R;
import com.mashang.stuManSystem.utils.T1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/class1")
@Api(tags = "班级模块")
public class ClazzController {

    @Autowired
    private IClazzService service;

    //查询班级详情
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "班级id", required = true, paramType = "query")
    })
    public R<Clazz> queryById(Integer classId){
        return R.success(service.getById(classId));
    }
    //添加班级
    @PostMapping
    public R<ClazzVo> insert(Clazz clazz){
        boolean b = false;
        try {
            b = service.save(clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.to(b);
    }
    //修改班级
    @PutMapping
    public R<ClazzVo> update(Clazz clazz){
        boolean b = false;
        try {
            b = service.updateById(clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.to(b);
    }
    //删除班级
    @DeleteMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "班级id", required = true, paramType = "query")
    })
    public R<ClazzVo> delete(Integer classId){
        return R.to(service.removeById(classId));
    }
    //查询班级列表
    @GetMapping("/list")
    public T1<List<Clazz>> queryList(String classNo, String name, String type, Integer pageNum, Integer pageSize){
        Page<Clazz> clazzPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Clazz> qw = new QueryWrapper<>();
        qw.eq(classNo != null && !classNo.equals(""), "class_no", classNo);
        qw.eq(name != null && !name.equals(""), "name", name);
        qw.eq(type != null && !type.equals(""), "type", type);
        Page<Clazz> clazzPage1 = service.page(clazzPage, qw);
        List<Clazz> records = clazzPage1.getRecords();
        long total = clazzPage1.getTotal();
        return T1.success(records, total);


    }
}
